package com.njqy.ticket.service.common.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njqy.ticket.common.Constants;
import com.njqy.ticket.dto.common.LoginUser;
import com.njqy.ticket.entity.common.UserEntity;
import com.njqy.ticket.mapper.common.MenuMapper;
import com.njqy.ticket.mapper.common.UserMapper;
import com.njqy.ticket.service.common.IMenuService;
import com.njqy.ticket.service.common.IRoleService;
import com.njqy.ticket.service.common.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Override
    public LoginUser userToLoginUser(String username) {
        //查询用户信息
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserEntity::getName, username).eq(UserEntity::getStatus, "0");
        UserEntity user = this.getOne(wrapper);
        if (Objects.isNull(user)) {
            throw new RuntimeException("用户不存在");
        }
        //封装权限信息
        // List<String> permissionList = getPermissionList(username);
        return new LoginUser(user, null);
    }

    @Override
    public List<String> getPermissionList(String username) {
        if(StringUtils.isBlank(username)){
            return null;
        }
        //查询用户信息
        String roleKey = roleService.getRoleKey(username);
        List<String> list;
        if(Constants.ADMIN_KEY.equals(roleKey)){
            list = menuService.getMenu();
        }else {
            list = menuService.getMenu(username);
        }
        return list;
    }
}
